<?php
namespace app\reportExport\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;

class TeacherLeaveApproval extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$page = input('page');
		$rows = input('rows');
		$startDate = input("startDate");
		$endDate = input("endDate");
		$status = input("status");
		$where = [];
		if($status) $where["LEAVE_STATUS"] = array("eq",$status);
		if($startDate && $endDate) {
			$where["CREATE_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["CREATE_TIME"] = array("egt", $startDate);
			if($endDate) $where["CREATE_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("T_TEACHER_LEAVE")
				  ->where($where)
				  ->page($page, $rows)
				  ->order("CREATE_TIME desc,UPDATE_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
			$leaveStatus = $obj["LEAVE_STATUS"];
			if("000" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "审批通过";
			else if("001" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "待审批";
			else if("002" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "审批不通过";
			else if("003" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "已撤消";
			$backStatus = $obj["BACK_STATUS"];
			if($backStatus) {
				if("000" == $backStatus) $obj["BACK_STATUS_TEXT"] = "审批通过";
				else if("001" == $backStatus) $obj["BACK_STATUS_TEXT"] = "待审批";
				else if("002" == $backStatus) $obj["BACK_STATUS_TEXT"] = "审批不通过";
				else if("003" == $backStatus) $obj["BACK_STATUS_TEXT"] = "已撤消";
			}
			$data[$key] = $obj;
		}
		$count = Db::table("T_TEACHER_LEAVE req")
				  ->where($where)
				  ->count();
		$grid["rows"] = $data;
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$page = input('page');
		$rows = input('rows');
		$startDate = input("startDate");
		$endDate = input("endDate");
		$status = input("status");
		$where = [];
		if($status) $where["LEAVE_STATUS"] = array("eq",$status);
		if($startDate && $endDate) {
			$where["CREATE_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["CREATE_TIME"] = array("egt", $startDate);
			if($endDate) $where["CREATE_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("T_TEACHER_LEAVE")
				  ->where($where)
				  ->page($page, $rows)
				  ->order("CREATE_TIME desc,UPDATE_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
			$leaveStatus = $obj["LEAVE_STATUS"];
			if("000" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "审批通过";
			else if("001" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "待审批";
			else if("002" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "审批不通过";
			else if("003" == $leaveStatus) $obj["LEAVE_STATUS_TEXT"] = "已撤消";
			$backStatus = $obj["BACK_STATUS"];
			if($backStatus) {
				if("000" == $backStatus) $obj["BACK_STATUS_TEXT"] = "审批通过";
				else if("001" == $backStatus) $obj["BACK_STATUS_TEXT"] = "待审批";
				else if("002" == $backStatus) $obj["BACK_STATUS_TEXT"] = "审批不通过";
				else if("003" == $backStatus) $obj["BACK_STATUS_TEXT"] = "已撤消";
			}
			$data[$key] = $obj;
		}
		//excel表头内容
		$header = array("申请老师"=>"TEACHER_NAME","请假类型"=>"LEAVE_TYPE","请假开始时间"=>"START_TIME","请假结束时间"=>"END_TIME","请假时长"=>"LEAVE_DAYS","请假原因"=>"LEAVE_REASON","审批状态"=>"LEAVE_STATUS_TEXT","申请时间"=>"CREATE_TIME","审批人"=>"APPROVER","驳回原因"=>"REJECT_REASON","留言"=>"MESSAGE","销假状态"=>"BACK_STATUS_TEXT","销假开始时间"=>"BACK_START_DATE","销假结束时间"=>"BACK_END_DATE","销假时长"=>"BACK_DAYS","请假期间工作量-正课节数"=>"TIME1_LESSON1","请假期间工作量-自习节数"=>"TIME1_LESSON2","请假期间工作量-选修课节数"=>"TIME1_LESSON3","请假期间工作量-值日天数"=>"TIME1_LESSON4","假前假后自己上课节（天）数-正课节数"=>"TIME2_LESSON1","假前假后自己上课节（天）数-自习节数"=>"TIME2_LESSON2","假前假后自己上课节（天）数-选修课节数"=>"TIME2_LESSON3","假前假后自己上课节（天）数-值日天数"=>"TIME2_LESSON4","替班人及替班节（天）数-正课节数"=>"TIME3_LESSON1","替班人及替班节（天）数-自习节数"=>"TIME3_LESSON2","替班人及替班节（天）数-选修课节数"=>"TIME3_LESSON3","替班人及替班节（天）数-值日天数"=>"TIME3_LESSON4","是否值日"=>"IS_DUTY","值日替班人"=>"DUTY_REPLACER","是否晚修"=>"IS_LATE","晚修替班人"=>"LATE_REPLACER","职务"=>"POSITION","职位委托人"=>"POSITION_CONSIGNER");
		Excel::toExcel($data,'教师请假申请表',$header);
		exit;
	}

}
